import React, {Component, useState} from 'react';
import {PageContainer, ProForm, ProFormUploadButton} from "@ant-design/pro-components";
import {Col, Row, Space, Typography, Card, Upload, Button, Form, message, Modal} from "antd";
import {
  GetUploadedFiles,
  NoDataAPI,
  SchoolPass,
  SchoolReject,
  UploadApi,
  UploadWordPath
} from "@/services/School/SchoolApi";
import {ExclamationCircleOutlined, UploadOutlined} from "@ant-design/icons";
import {history} from "@@/core/history";
import {ServerMessages} from "@/services/ServerMessages";
import {DownloadByNameApi} from "@/services/Province/ProvinceApi";
import {ShowError} from "@/utils/format";
const path = []
let loading = true


class Situation extends Component {

  state = {
    download: "2_2_2_2",
    downloadList: []
  }

  render() {
    const status_is_show = (history.location.query.flag === 'display') || (history.location.query.id && history.location.query.fill_id)
    const status_is_check = history.location.query.flag === 'check'
    const is_seen = history.location.query.is_seen

    const handleCheck = () => {
      SchoolPass(history.location.query.id).then(response => {
        console.log(response)
        if (response.status === 0) {
          message.success(response.message)
          history.goBack()
        } else {
          ShowError(response.message)
        }
      })
      // history.goBack()
    }

    const { confirm } = Modal;


    const showConfirm = () => {
      confirm({
        title: '您确认驳回吗?',
        icon: <ExclamationCircleOutlined />,
        content: '驳回后，学科用户可重新填报。',
        onOk() {
          SchoolReject(history.location.query.id).then(response => {
            if (response.status === 0) {
              message.success(response.message)
              history.goBack()
            } else {
              ShowError(response.message)
            }
          }, () => {
            ShowError(ServerMessages.error_message)
          })
        },
        onCancel() {
          console.log('Cancel');
        },
      });
    };


    const handleBack = () => {
      showConfirm()
      // SchoolReject(history.location.query.fill_id).then(response => {
      //   if (response.status === 0) {
      //
      //   } else {
      //     ShowError(response.message)
      //   }
      // })
    }

    return (
      <PageContainer
      title="一流本科专业建设情况"
      content={<Typography.Text type="danger">2021年1月1日至今，学科关于国家一流本科专业、省级一流本科专业建设及“四新”专业建设情况简述。<br/>仅填写当前填报周期内新增的数据，如有删减，请告知学校管理员。</Typography.Text>}>
        <Card bordered={false}>
          <ProForm
            request={() => {
              return new Promise(resolve => {
                if (status_is_check) {
                  GetUploadedFiles('/api/index/school-query/review-download-query-wordname', history.location.query.id).then(response => {
                    if (response.status === 0) {
                      console.log(response.filenameList)
                      this.setState({downloadList:response.filenameList})
                    }
                  })
                } else if (status_is_show) {
                  GetUploadedFiles('/api/wordname-query-download', this.state.download).then(response => {
                    if (response.status === 0) {
                      console.log(response.filenameList)
                      this.setState({downloadList:response.filenameList})
                    }
                  })
                }
                resolve(true)
              })
            }}
            onFinish={() => {
              return new Promise(resolve => {

                if (loading) {
                  resolve(true)
                } else {
                  console.log("@@path", path)
                  UploadWordPath('/api/index/cultivate-talents/progress/major-class/major-class-situation', path).then(response => {
                    console.log("@返回数据", response)
                    if (response.status === 0) {
                      message.success(response.message)
                      history.goBack()
                      resolve(true)
                    } else {
                      ShowError(response.message)
                      history.goBack()
                      resolve(true)
                    }
                  }, () => {
                    ShowError(ServerMessages.error_message)
                    resolve(true)
                  })
                }

              })
            }}
            hideRequiredMark
            style={{ margin: 'auto', marginTop: 8 }}
            name="basic"
            layout="horizontal"
            labelCol={{ span: 8 }}
            submitter={{
              render: (props, doms) => {
                if (status_is_check && is_seen === '0') {
                  return <Row>
                    <Col offset={8}>
                      <Space>
                        <Button onClick={() => { history.goBack() }}>返回</Button>
                        <Button onClick={handleCheck} type="primary">通过</Button>
                        <Button danger onClick={handleBack}>驳回</Button>
                        <Button disabled>已提交</Button>
                      </Space>
                    </Col>
                  </Row>
                } else if (status_is_check && is_seen === '1') {
                  return <Row>
                    <Col offset={8}>
                      <Space>
                        <Button onClick={() => { history.goBack() }}>返回</Button>
                        <Button disabled>已审核</Button>
                      </Space>
                    </Col>
                  </Row>
                } else if (status_is_show) {
                  return (
                    <Row>
                      <Col offset={8}>
                        <Button onClick={() => { history.goBack() }}>返回</Button>
                        <Button disabled>已提交</Button>
                      </Col>
                    </Row>
                  )
                }
                return (
                  <Row>
                    <Col offset={8}>
                      <Space>
                        <Button onClick={() => { history.goBack() }}>返回</Button>
                        <Button onClick={() => {
                          NoDataAPI(this.state.download).then(res => {
                            if (res.status === 0) {
                              message.success(res.message)
                              history.goBack()
                            } else {
                              ShowError("提交失败，请稍后重试.")
                            }
                          }, () => {
                            ShowError(ServerMessages.error_message)
                          })
                        }
                        } type="default">无相关数据</Button>

                        <Button onClick={() => {
                          window.location.reload()
                        }}>重置</Button> {doms}</Space>
                    </Col>
                  </Row>
                );
              },
              resetButtonProps: {
                style: {
                  // 隐藏重置按钮
                  display: 'none',
                },
              },
              submitButtonProps: {
                style: {
                  marginLeft: '-8px'
                }
              },
            }}>
            {!status_is_show && (
              <Form.Item label="请上传相关文档">
              <Upload {...UploadApi('/api/index/cultivate-talents/progress/major-class/major-class-situation-template', (response) => {
                try {
                  path.push(response.file.response.data[0].path)
                  console.log(path.length, response.fileList.length, path.length === response.fileList.length)
                  if (path.length === response.fileList.length) {
                    console.log("@loading false!")
                    loading = false
                  }
                  if (path.length === 0) {
                    loading = true
                  }
                } catch {
                  ShowError('上传失败')
                }
              })}>
                <Button type="default" icon={<UploadOutlined/>}>上传文档</Button>
              </Upload>
            </Form.Item>
            )}
            {status_is_show && (
              <Space>
                {this.state.downloadList.map((item, idx) => {
                  return <Button onClick={() => {
                    DownloadByNameApi(item)
                  }
                  } key={item}>下载文件{idx+1}</Button>
                })}
              </Space>
            )}
          </ProForm>
        </Card>
      </PageContainer>
    );
  }
}

export default Situation;
